import './index.scss';
import tpl from './index.tpl';

export default () => {
  return {
    name: 'toast',
    showToast (opt) {
      //check toast is exist
      const oToastBox = document.getElementsByClassName('toast-box')[0]
      if(oToastBox){
        return;
      }

      const oToast = document.createElement('div');
      oToast.className = 'toast-box';
      oToast.innerHTML = tpl().replace(/{{(.*?)}}/g, (node, key) => {
        return {
          icon: opt.icon === 'warning' ? 'warning' : 'check',
          text: opt.title
        }[key];
      });
      
      document.body.appendChild(oToast);

      //remove ele
      setTimeout(()=>{
        const oToast = document.getElementsByClassName('toast-box')[0];
        oToast.remove();     
      }, opt.duration || 2000);
    }
  }
}